package com.glodon.paas.account.dao.mysql;

import org.springframework.beans.factory.annotation.Autowired;

import net.oauth.OAuthConsumer;

import com.glodon.paas.account.dao.OAuthConsumerDao;
import com.glodon.paas.account.dao.domain.ConsumerEntity;
import com.glodon.paas.account.dao.mapper.ConsumerEntityMapper;

public class OAuthConsumerDaoMysqlImpl implements OAuthConsumerDao {
	@Autowired
	private ConsumerEntityMapper consumerEntityMapper;
	
	@Override
	public String insert(ConsumerEntity entity) {
		consumerEntityMapper.insert(entity);
		return entity.getId();
	}

	@Override
	public ConsumerEntity get(String id) {
		return consumerEntityMapper.selectByPrimaryKey(id);
	}

	@Override
	public int delete(String id) {
		return consumerEntityMapper.deleteByPrimaryKey(id);
	}

	@Override
	public OAuthConsumer readConsumer(String consumerKey) {
		ConsumerEntity c = consumerEntityMapper.selectByConsumerKey(consumerKey);
		if(c!=null){
			 OAuthConsumer consumer = new OAuthConsumer(c.getCallbackURL(), c.getConsumerkey(), c.getConsumerSecrect(), null);
            consumer.setProperty("name", c.getConsumerkey());
            consumer.setProperty("description", c.getDescription());
			return consumer;
		}
		else{
			return null;
		}
	}
}
